<?php
namespace app\admin\controller;

use think\Controller;
use app\admin\model\Job;
use app\admin\model\User;
use think\Request;

class Desk extends Rbac
{
//    前台管理页面
    public function desk()
    {
        $info = Job::where('job_jointime<>\'\'')->select();
        $count = count($info);
        $info = Job::where('job_jointime<>\'\'')->paginate(10,$count);
        $this->assign('info',$info);
        return $this->fetch('desk/desk');
    }
//    前台搜索
    public function search(Request $req){
        if($req->post()){
            $job_phone = $req->post('job_phone');
            $info = Job::where('job_phone like \'%'.$job_phone.'%\'')
                ->where('job_jointime<>\'\'')
                ->select();
            $count = count($info);
            $info = Job::where('job_phone like \'%'.$job_phone.'%\'')
                ->where('job_jointime<>\'\'')
                ->paginate(10,$count);
            $this->assign('info',$info);
            return $this->fetch('desk/desk');
        }
    }
//    前台查看详情
    public function test(){
        $job_id = request()->param('job_id');
        $result = Job::get($job_id)->toArray();
        $this->assign('result',$result);
        return $this->fetch('desk/test');
    }
//    前台分配咨询师
    public function allot(Request $req){
        if($req->post()){
            $user_name = $req->post('user_name');
            $job_id = $_SESSION['job_id'];
            $data = [
                'job_inter_man'=>$user_name
            ];
            $res = Job::where('job_id in('.$job_id.')')->update($data);
            if($res>=0){
                echo "<script>parent.layer.msg('分配成功',{icon:1,time:2000});var index = parent.layer.getFrameIndex(window.name);parent.layer.close(index); setTimeout(function () {main.location.href='admin/Desk/desk';
window.parent.location.reload();},2000);</script>";

            }else{
                $this->error('分配失败！');
            }
            unset($_SESSION['job_id']);
        }
        $_SESSION['job_id'] = $req->param('job_id');
        $info = User::where('`user_r_id`=6 or user_r_id=7')
            ->field('user_id,user_name')
            ->select();
        $this->assign('res',$info);
        return $this->fetch('desk/allot');
    }
//   修改到达状态
    public function isCome(Request $req){
        $job_get_sn = $req->post('job_get_sn');
        $job_id = $req->post('job_id');
        $res = Job::where('job_id='.$job_id)->update([
            'job_get_sn'=>1,
            'job_get_time'=>time()
        ]);
        if($res>=0){
            return 200;
        }else{
            return 100;
        }

    }

}
